package com.memory.servlet;

import com.memory.dao.impl.UserDaoImpl;
import com.memory.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * ClassName: login
 * Package: com.memory.servlet
 * Description:
 *
 * @Author Memory
 * @Create 2024/8/14 14:31
 * @Version 1.0
 */
@WebServlet(urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {
    UserDaoImpl userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.插入学生记录
        UserDaoImpl userDao = new UserDaoImpl();
        List<User> userList = null;

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        try {
            User user = new User();
            Integer sno =Integer.parseInt(request.getParameter("Sno")) ;

            user.setSno(sno);

            String sname = request.getParameter("Sname");
            user.setSname(sname);

            String ssex = request.getParameter("Ssex");
            user.setSsex(ssex);

            Integer sage =Integer.parseInt(request.getParameter("Sage")) ;
            user.setSage(sage);

            String sdept = request.getParameter("Sdept");
            user.setSdept(sdept);

            // 插入数据
            userDao.insert(user);
            userList = userDao.selectAll();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("userList", userList);

        request.getRequestDispatcher("userList.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
